<?php
#
#
# Copyright TEDECO, 2009. http://tedeco.fi.upm.es
#
# This file is part of kumenya.
#
# Kumenya is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Kumenya is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
#
#
#
?>
<?php
	Interfaz::CheckSession(Accountant);

	Interfaz::Record();

	$AccountsBalance = Interfaz::Data("Accounts");
	$SubaccountBalances = Interfaz::Data("Subaccount");
	

	if (!isset($AccountsBalance) && !isset($SubaccountBalances)) {
		Interfaz::Error("You must choose at least one option");
		$result = false;
	}
	else {		
		// HERE ARE SAVED THE BALANCE'S RESULTS
		$balanceSheetAccountIncomes = array();
		$balanceSheetSubAccountIncomes = array();
		$balanceSheetAccountExpenses = array();
		$balanceSheetSubAccountExpenses = array();
		
		// HERE ARE SAVED THE DETAILS OF EACH ACCOUNT OR CLASS
		$accountIncomesDetails = array();
		$subAccountIncomesDetails = array();
		$accountExpensesDetails = array();
		$subAccountExpensesDetails = array();
	
		$classes = AccountingAccount::GetIncomeClasses();
		foreach ($classes as $class) {
			if (isset($AccountsBalance) || isset($SubaccountBalances)) {
				$accounts = AccountingAccount::GetAccountIncomes($class);

				// ACCOUNT INCOMES
				foreach ($accounts as $account) {
					$criterias = array_merge (Interfaz::Data(), $account);

					// IF IT'S ONLY OF ACCOUNTS ADD ALL THE ENTRIES
					if ($SubaccountBalances)
						$all = array (All => No);
					else
						$all = array (All => Yes);						
					$criterias = array_merge ($criterias, $all);

					// ACCOUNT DETAILS 
					$balanceSheetAccountIncomes[$account[Uid]] = AccountingAccount::AccountIncomes($criterias);
					$accountIncomesDetails[$account[Uid]] = AccountingAccount::SeeDetails($criterias[ThirdType], $criterias);

					if ($SubaccountBalances) {
						$subaccounts = AccountingAccount::GetSubaccountsIncome($account);
						foreach ($subaccounts as $subaccount) {
							$criterias = array_merge (Interfaz::Data(), $subaccount);
							// SUBACCOUNT DETAILS 
							$balanceSheetSubAccountIncomes[$account[Uid]][$subaccount[Uid]] = AccountingAccount::SubaccountIncomes($criterias);
							$subAccountIncomesDetails[$subaccount[Uid]] = AccountingAccount::SeeDetails($criterias[ThirdType], $criterias);
						}
					}
				}
			}
		}

		if (isset ($balanceSheetAccountIncomes[$account[Uid]])) {
			$header = $balanceSheetAccountIncomes[$account[Uid]];
			$headerDetails = $accountIncomesDetails[$account[Uid]];
		}
				
		$classes = AccountingAccount::GetClassesExpenses();
		foreach ($classes as $class) {
			if (isset($AccountsBalance) || isset($SubaccountBalances)) {
				$accounts = AccountingAccount::GetAccountsExpenses($class);

				// EXPENSES ACCOUNTS
				foreach ($accounts as $account) {
					$criterias = array_merge (Interfaz::Data(), $account);

					// IF IT'S ONLY OF ACCOUNTS ADD ALL THE ENTRIES
					if ($SubaccountBalances)
						$all = array (All => No);
					else
						$all = array (All => Yes);						
					$criterias = array_merge ($criterias, $all);

					// ACCOUNT DETAILS
					$balanceSheetAccountExpenses[$account[Uid]] = AccountingAccount::AccountsExpenses($criterias);
					$accountExpensesDetails[$account[Uid]] = AccountingAccount::SeeDetails($criterias[ThirdType], $criterias);

					if ($SubaccountBalances) {
						$subaccounts = AccountingAccount::GetSubaccountsExpenses($account);
						foreach ($subaccounts as $subaccount) {
							$criterias = array_merge (Interfaz::Data(), $subaccount);
							// SUBACCOUNT DETAILS
							$balanceSheetSubAccountExpenses[$account[Uid]][$subaccount[Uid]] = AccountingAccount::SubaccountsExpenses($criterias);
							$subAccountExpensesDetails[$subaccount[Uid]] = AccountingAccount::SeeDetails($criterias[ThirdType], $criterias);
						}
					}
				}
				
			}
		}

		if (isset ($balanceSheetAccountExpenses[$account[Uid]])) {
			$header = $balanceSheetAccountExpenses[$account[Uid]];
			$headerDetails = $accountExpensesDetails[$account[Uid]];
		}
		
		include "ResultadoBalanceIngresosGastosExcel.php";
		$result = TRUE;

	}
?>